package com.monster.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * cookie工具类
 * @author kuang
 */
public class CookieUtils {
    public CookieUtils () {}

    /**
     * 设置HTTP Cookie
     * @param response 响应对象
     * @param key cookie 的key
     * @param value cookie 的值
     */
    public void setCookie(HttpServletResponse response, String key, String value, boolean secure, boolean httpOnly, Integer timeOut) {
        // 创建一个 cookie对象
        Cookie cookie = new Cookie(key, value);
        // 设置过期时间
        cookie.setMaxAge(timeOut == null ? 24 * 60 * 60 : timeOut);
        // Https 安全cookie
        cookie.setSecure(secure);
        // 
        cookie.setHttpOnly(httpOnly);
        //将cookie对象加入response响应
        response.addCookie(cookie);
    }
}
